package com.rc.saas.tenant.controller.elec;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.base.TenantBaseController;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.elec.ElecStation;
import com.rc.saas.tenant.model.fault.RcElecPileFaultRecord;
import com.rc.saas.tenant.model.tenant.TenantRole;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.search.elec.ElecGunSearch;
import com.rc.saas.tenant.search.fault.RcElecPileFaultRecordSearch;
import com.rc.saas.tenant.service.elec.ElecGunService;
import com.rc.saas.tenant.service.elec.ElecStationService;
import com.rc.saas.tenant.service.tenant.TenantRoleService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.util.ExportUtils;
import com.rc.saas.tenant.vo.gun.ElecGunMonitorVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yang
 * @date 2022/06/10
 */
@Controller
@RequestMapping("/pileMonitor")
public class ElecPileMonitorController extends TenantBaseController {
    private static Logger logger = LogManager.getLogger(ElecPileMonitorController.class);

    @Autowired
    private ElecGunService elecGunService;

    @Autowired
    private ElecStationService elecStationService;

    @Autowired
    private TenantRoleService tenantRoleService;

    /**
     * 跳转桩页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String elecPilePage(Model model ) {
        //获取登陆人信息
        TenantUser user = ShiroUtils.getSessionUser();
        TenantRole role = tenantRoleService.selectByPrimaryKey(user.getTenantRoleId());
        if (null == user || null == role || "3".equals(role.getRoleType())) {
            List<ElecStation> elecStationList = new ArrayList<>();
            model.addAttribute("elecStationList", elecStationList);
            return "/templates/elec/elecPileMonitor";
        }
        String dataAuth = role.getDataAuth();//数据权限 100 平台权限, 010 加盟商权限,001企业权限,111全部权限 默认111 不能为000
        String roleType = role.getRoleType();//角色类型 ( 0 超级管理员 1 普通用户 2加盟商 3企业用户) 默认1`
        BaseSearch baseSearch = new BaseSearch();
        baseSearch.setDataAuth(dataAuth);
        baseSearch.setRoleType(roleType);
        baseSearch.setBaseOperatorId(user.getOperatorId());
        baseSearch.setTenantCode(user.getTenantCode());

        List<ElecStation> elecStationList = elecStationService.listStationAndLatiLong(user.getTenantCode(), baseSearch);
        model.addAttribute("elecStationList", elecStationList);
        if (null!=elecStationList && !elecStationList.isEmpty())
            model.addAttribute("stationNo", elecStationList.get(0).getStationNo());
        else
            model.addAttribute("stationNo", "00000000");
        return "/templates/elec/elecPileMonitor";
    }

    /**
     * 充电桩运行监控-》在线监控
     *
     * @return
     */
    @RequestMapping(value = "/listPagePile", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPagePile(ElecGunSearch elecGunSearch) {
        //获取登陆人信息
        TenantUser user = ShiroUtils.getSessionUser();
        TenantRole role = tenantRoleService.selectByPrimaryKey(user.getTenantRoleId());
        if (null == user || null == role || "3".equals(role.getRoleType())) {
            PageInfo<ElecGunMonitorVo> pageInfo = new PageInfo<>();
            WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
            return webPageInfo;
        }
        String dataAuth = role.getDataAuth();//数据权限 100 平台权限, 010 加盟商权限,001企业权限,111全部权限 默认111 不能为000
        String roleType = role.getRoleType();//角色类型 ( 0 超级管理员 1 普通用户 2加盟商 3企业用户) 默认1`
        elecGunSearch.setDataAuth(dataAuth);
        elecGunSearch.setRoleType(roleType);
        elecGunSearch.setBaseOperatorId(user.getOperatorId());

        PageInfo<ElecGunMonitorVo> pageInfo = elecGunService.listPageByMonitor(elecGunSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 在线监控-》返回折线图
     *
     * @param stationNo 站点名称/编号
     * @param type      0日 1月 默认0
     * @return
     */
    @RequestMapping(value = "/listPageLintChart", method = RequestMethod.GET)
    @ResponseBody
    public TResponse listPageLintChart(String stationNo, String type) {
        try {
            return elecGunService.listPageLintChart(stationNo, type);
        } catch (Exception e) {
            e.printStackTrace();
            return TResponse.FAIL();
        }
    }


    /**
     * 跳转桩页面
     *
     * @return
     */
    @RequestMapping(value = "/map/index", method = RequestMethod.GET)
    public String map() {
        return "/templates/elec/temp3";
    }


    /**
     * 导出在线监控数据
     */
    @ResponseBody
    @RequestMapping("/exporListStationAndLatiLong")
    public TResponse exporListStationAndLatiLong(ElecGunSearch search, HttpServletResponse response) {
        //导出拼接开始条数
        try {
            //获取登陆人信息
            TenantUser user = ShiroUtils.getSessionUser();
            TenantRole role = tenantRoleService.selectByPrimaryKey(user.getTenantRoleId());
            String dataAuth = role.getDataAuth();//数据权限 100 平台权限, 010 加盟商权限,001企业权限,111全部权限 默认111 不能为000
            String roleType = role.getRoleType();//角色类型 ( 0 超级管理员 1 普通用户 2加盟商 3企业用户) 默认1`
            search.setDataAuth(dataAuth);
            search.setRoleType(roleType);
            search.setBaseOperatorId(user.getOperatorId());

            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            ExportUtils.setDefaultPaging(search);
            PageInfo<ElecGunMonitorVo> pageInfo = elecGunService.listPageByMonitor(search);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("在线监控列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), ElecGunMonitorVo.class).sheet("在线监控列表").doWrite(pageInfo.getList());
            return null;
        } catch (Exception e) {
            logger.error("导出失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
    }

}
